package com.prs.crm.action.pm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.pm.PmSource;
import com.prs.crm.service.pm.PmSourceService;
import com.prs.crm.service.sys.LogService;

public class PmSourceAction extends BaseAction {
	private PmSource pmSource;
	private PmSourceService pmSourceService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listPmSource")
	public String listPmSource() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PmSource.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmSourceService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editPmSource")
	public String editPmSource() {
		return SUCCESS;
	}

	@Action(value = "submitPmSource", results = {
			@Result(name = "success", type = "redirect", location = "listPmSource"),
			@Result(name = "error", type = "chain", location = "editPmSource") })
	public String submitPmSource() {
		if (pmSource.getName() == null || pmSource.getName().trim().equals("")) {
			this.addActionError("请正确填写项目来源名称");
			return ERROR;
		}
		if (this.getLogService().getSearchName(PmSource.class,
				pmSource.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getPmSourceService().save(pmSource);
		return SUCCESS;
	}

	@Action(value = "removePmSource", results = {
			@Result(name = "success", type = "redirect", location = "listPmSource"),
			@Result(name = "error", type = "chain", location = "listPmSource") })
	public String removePmSource() {
		if (ids == null) {
			this.addActionError("请选择要删除的项目来源");
			return ERROR;
		}
		for (Integer id : ids) {
			PmSource source = this.getPmSourceService().get(id);
			if (source.getProjects().size() != 0) {
				this.addActionError("已有项目引用，不能删除");
				return ERROR;
			}
		}
		this.getPmSourceService().batchRemove(ids);
		return SUCCESS;
	}

	public PmSource getPmSource() {
		return pmSource;
	}

	public void setPmSource(PmSource pmSource) {
		this.pmSource = pmSource;
	}

	public PmSourceService getPmSourceService() {
		return pmSourceService;
	}

	public void setPmSourceService(PmSourceService pmSourceService) {
		this.pmSourceService = pmSourceService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
